package com.shitou.shitoumember.controller;

import com.shitou.shitoucommons.annotation.CheckUserLogin;
import com.shitou.shitoucommons.utils.PageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.shitou.shitoumember.entity.MemberReceiveAddressEntity;
import com.shitou.shitoumember.service.MemberReceiveAddressService;
import com.shitou.shitoucommons.utils.RespBean;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员收货地址
 *
 * @author shitouren
 * @email 3076767823@qq.com
 * @date 2023-03-28 16:52:06
 */
@RestController
@RequestMapping("shitoumember/memberreceiveaddress")
public class MemberReceiveAddressController {
    @Autowired
    private MemberReceiveAddressService memberReceiveAddressService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public RespBean list(@RequestParam Map<String, Object> params){
        PageUtils page = memberReceiveAddressService.queryPage(params);

        return RespBean.success().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public RespBean info(@PathVariable("id") Long id){
        MemberReceiveAddressEntity memberReceiveAddress = memberReceiveAddressService.getById(id);

        return RespBean.success().put("memberReceiveAddress", memberReceiveAddress);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public RespBean save(@RequestBody MemberReceiveAddressEntity memberReceiveAddress){
        memberReceiveAddressService.save(memberReceiveAddress);

        return RespBean.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public RespBean update(@RequestBody MemberReceiveAddressEntity memberReceiveAddress){
        memberReceiveAddressService.updateById(memberReceiveAddress);

        return RespBean.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public RespBean delete(@RequestBody Long[] ids){
        memberReceiveAddressService.removeByIds(Arrays.asList(ids));

        return RespBean.success();
    }

    /**
     * 添加收获地址
     * @param receiveAddress 地址实体类
     * @return RespBean
     */
    @ApiOperation(value = "添加用户的收货地址，需要登陆校验")
    @CheckUserLogin
    @PostMapping("/add/address")
    public RespBean addAddress(@RequestBody MemberReceiveAddressEntity receiveAddress){
        return memberReceiveAddressService.addAddress(receiveAddress);
    }


    /**
     * 设置默认地址
     * @param receiveAddress 地址实体类
     * @return RespBean
     */
    @ApiOperation(value = "用户设置默认地址时调用，需要登陆校验")
    @CheckUserLogin
    @PostMapping("/setDefaultAddress")
    public RespBean setDefaultAddress(@RequestBody MemberReceiveAddressEntity receiveAddress){
        return memberReceiveAddressService.setDefaultAddress(receiveAddress);
    }


    /**
     * 获得该会员的所有地址
     * @param receiveAddress 地址实体类
     * @return RespBean
     */
    @ApiOperation(value = "回去当前登录会员的所有地址信息，需要登陆校验")
    @CheckUserLogin
    @PostMapping("/memberAddress")
    public RespBean memberAddress(@RequestBody MemberReceiveAddressEntity receiveAddress){
        return memberReceiveAddressService.getMemberAddress(receiveAddress);
    }


    /**
     * 更新地址信息
     * @param receiveAddress 地址实体类
     * @return RespBean
     */
    @ApiOperation(value = "用户对单个地址进行更新时调用，需要登陆校验")
    @CheckUserLogin
    @PostMapping("/updateAddress")
    public RespBean updateAddress(@RequestBody MemberReceiveAddressEntity receiveAddress){
        return memberReceiveAddressService.updateAddress(receiveAddress);
    }


    /**
     * 删除地址信息通过id
     * @param receiveAddress 地址实体类
     * @return RespBean
     */
    @ApiOperation(value = "用户通过地址的id对单个地址进行删除,需要登陆校验")
    @CheckUserLogin
    @PostMapping("/deleteAddress")
    public RespBean deleteAddress(@RequestBody MemberReceiveAddressEntity receiveAddress){
        return memberReceiveAddressService.deleteAddress(receiveAddress);
    }


    /**
     * 远程调用获取地址信息
     * @param addressId 地址id
     * @return RespBean
     */
    @ApiOperation(value = "远程调用，获取用户的地址信息")
    @CheckUserLogin(value = false)
    @PostMapping("/getAddressVo")
    public RespBean getAddressVo(@RequestParam long addressId, @RequestParam long memberId){
        return memberReceiveAddressService.getAddressVo(addressId, memberId);
    }


    /**
     * 远程调用，返回该地址是否是该用户的
     * @param memberId
     * @param addressId
     * @return
     */
    @ApiOperation(value = "远程调用，通过地址id判断当前地址是否是属于该会员")
    @PostMapping("/checkMemberAddress")
    public RespBean checkMemberAddress(@RequestParam Long memberId, @RequestParam Long addressId){
        return memberReceiveAddressService.checkMemberAddress(memberId, addressId);
    }

}
